package com.mifengs.mall.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.mifengs.commonbase.util.PreferenceUtils;
import com.mifengs.mall.Constant;
import com.mifengs.mall.R;
import com.mifengs.mall.ui.login.LoginActivity;
import com.mifengs.mall.util.ToIntentUtil;

/**
 * Created by lynn on 2018/1/3.
 */

public class SubAdapter extends DelegateAdapter.Adapter<SubAdapter.MainViewHolder> {

    private Context mContext;

    private LayoutHelper mLayoutHelper;

    private VirtualLayoutManager.LayoutParams mLayoutParams;

    public SubAdapter(Context context, LayoutHelper layoutHelper) {
        this(context, layoutHelper, new VirtualLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 300));
    }

    public SubAdapter(Context context, LayoutHelper layoutHelper, @NonNull VirtualLayoutManager.LayoutParams layoutParams) {
        this.mContext = context;
        this.mLayoutHelper = layoutHelper;
        this.mLayoutParams = layoutParams;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    @Override
    public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MainViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item, parent, false));
    }

    @Override
    public void onBindViewHolder(MainViewHolder holder, int position) {
        // only vertical
        holder.itemView.setLayoutParams(new VirtualLayoutManager.LayoutParams(mLayoutParams));
        if(holder instanceof MainViewHolder){
            MainViewHolder mMainViewHolder = holder;
            mMainViewHolder.img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(PreferenceUtils.getLoginStatus()){
                        ToIntentUtil.toCommonIntent(Constant.H5_sign,mContext);
                    }else{
                        mContext.startActivity(new Intent(mContext, LoginActivity.class));
                    }

                }
            });
        }
    }


    @Override
    protected void onBindViewHolderWithOffset(MainViewHolder holder, int position, int offsetTotal) {
//        ((TextView) holder.itemView.findViewById(R.id.title)).setText(Integer.toString(offsetTotal));
    }

    @Override
    public int getItemCount() {
        return 1;
    }


public class MainViewHolder extends RecyclerView.ViewHolder {

    ImageView img;
    public MainViewHolder(View itemView) {
        super(itemView);
        img = (ImageView)itemView.findViewById(R.id.img);
    }
}
}
